var page=0;
var couponId = 1;//满减券
var couponNum = 0;
var couponState = 1; //有效
var overdue = 1; //未过期

$(function () {
    doNum();
    doAjax();
})

function doNum() {
    $.ajax({
        url:'/userCouponC/getCouponNumber.json',
        type:'json',
        data:{
            token:getItem('token'),
            couponId:couponId,
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                $('#nouse').html('未使用('+data.datas.youxiao+')');
                $('#hasuse').html('已使用('+data.datas.yishiyong+')')
                $('#overdue').html('已过期('+data.datas.yiguoqi+')')
            }
        }
    })
}

function doAjax() {
    page++;
    $.ajax({
        url:'/userCouponC/searchUserCoupon.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            couponId:couponId,
            page:page,
            state:couponState,
            overdue:overdue,
        },
        type:'post',
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.coupon_box'));
            }
        },
        success:function (data) {
            if(data.code==1){
                if(page==1){
                    $('.coupon_box').empty();
                    if(data.datas.datas==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('.coupon_box'));
                    }
                }
                var couponData = data.datas.datas;
                var ifUsed = '';
                for(var i=0;i<couponData.length;i++){
                    if(data.datas.flag==0){
                        ifUsed = '<div class="overdue"><img src="../img/overdue.png" alt="已过期"></div>'
                    }else{
                        ifUsed = '';
                    }
                    var stateImg = '';
                    if(data.datas.flag==1){
                        stateImg = 'background:url(../img/coupon_bg.jpg) no-repeat 0 0;background-size:cover;'
                    }else{
                        stateImg = 'background:url(../img/coupon_bg2.jpg) no-repeat 0 0;background-size:cover;'
                    }
                    if(couponId==1){
                        $('<div class="coupon">\n' +
                            '<div class="coupon_left" style="'+stateImg+'">\n' +
                            '<div class="full_money">满'+couponData[i].orderMoney+'可用</div>\n' +
                            '<div class="decrease_money">'+couponData[i].minusMoney+'</div></div>\n' +
                            '<div class="coupon_right">\n' +
                            '<div class="coupon_name">'+couponData[i].shopName+'</div>\n' +
                            '<div class="coupon_time">'+couponData[i].starTime+'_'+couponData[i].endTime+'</div>\n' +
                            '<div class="coupon_img"><img src="/qrcodeC/encode/'+couponData[i].id+'.jpeg" alt=""></div>\n' +
                            ' </div>'+ifUsed+'</div>').appendTo($('.coupon_box'))
                    }else{
                        $('<div class="coupon">\n' +
                            '<div class="coupon_left" style="background:url(../img/coupon_bg3.png);background-size: cover;">\n' +
                            '<div class="full_money"></div>\n' +
                            '<div class="decrease_money"></div></div>\n' +
                            '<div class="coupon_right">\n' +
                            '<div class="coupon_name">'+couponData[i].name+'</div>\n' +
                            '<div class="coupon_time">'+couponData[i].starTime+'_'+couponData[i].endTime+'</div>\n' +
                            ' </div></div>').appendTo($('.coupon_box'))
                    }
                }
                couponNum = couponData.length;
            }
        }
    })
    return couponNum;
}

//分类卡券点击
mui(".coupon_sort ul").on('tap','li',function(){
    $('.coupon_box').empty();
    if($(this).attr('value')=='2'){
        $('.use_state ul li').removeClass('use_act');
        $('.use_state ul li:first-child').addClass('use_act');
        overdue = 1;
        couponState = 1;
    }
    $(this).addClass('sort_act');
    $(this).siblings().removeClass('sort_act');
    couponId = $(this).attr('value');
    page=0;
    doNum();
    doAjax();
})

//卡券状态点击
mui(".use_state ul").on('tap','li',function(){
    $('.coupon_box').empty();
    $(this).addClass('use_act');
    $(this).siblings().removeClass('use_act');
    couponState = $(this).attr('couponState');
    overdue = $(this).attr('overdue');
    page=0;
    doAjax();
})


//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doAjax();
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});